package com.ibeeking.found.content.cp.rest.service.impl;

import com.ibeeking.found.content.cp.rest.service.IBroadcastGoodsService;
import com.ibeeking.found.content.cp.rest.service.IBroadcastService;
import com.ibeeking.found.content.service.common.dos.BroadcastGoodsDO;
import com.ibeeking.found.content.service.common.param.BroadcastGoodsParam;
import com.ibeeking.found.content.service.common.query.BroadcastGoodsQuery;
import com.ibeeking.found.content.service.common.vo.BroadcastGoodsVO;
import com.ibeeking.found.content.service.mapper.BroadcastGoodsMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName BroadcastGoodsServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2022-02-18 14:14
 **/
@Service
public class BroadcastGoodsServiceImpl extends ServiceImpl<BroadcastGoodsMapper, BroadcastGoodsDO> implements IBroadcastGoodsService {

    @Resource
    private BroadcastGoodsMapper broadcastGoodsMapper;
    @Resource
    private IBroadcastService broadcastServiceImpl;

    @Override
    public List<BroadcastGoodsVO> list(BroadcastGoodsQuery broadcastGoodsQuery) {
        List<BroadcastGoodsDO> broadcastGoodsDOs = broadcastGoodsMapper.selectList(new LambdaQueryWrapper<BroadcastGoodsDO>()
                .eq(broadcastGoodsQuery.getId() != null, BroadcastGoodsDO::getId, broadcastGoodsQuery.getId())
                .in(Collections3.isNotEmpty(broadcastGoodsQuery.getIds()), BroadcastGoodsDO::getId, broadcastGoodsQuery.getIds())
                .like(StringUtils.isNotBlank(broadcastGoodsQuery.getGoodsName()), BroadcastGoodsDO::getGoodsName, broadcastGoodsQuery.getGoodsName())
                .orderByDesc(BroadcastGoodsDO::getCreateTime)
        );
        return JsonUtils.jsonToList(broadcastGoodsDOs, BroadcastGoodsVO.class);
    }

    @Override
    public BroadcastGoodsVO view(Long id) {
        BroadcastGoodsDO broadcastGoodsDO = broadcastGoodsMapper.selectById(id);
        return BeanUtil.convertBean(broadcastGoodsDO, BroadcastGoodsVO.class);
    }

    /**
     * 校验唯一性
     *
     * @param param
     */
    public void checkUnique(BroadcastGoodsParam param) {
        LambdaQueryWrapper<BroadcastGoodsDO> queryWrapper = new LambdaQueryWrapper<BroadcastGoodsDO>()
                .ne(param.getId() != null, BroadcastGoodsDO::getId, param.getId())
                .eq(StringUtils.isNotBlank(param.getGoodsName()), BroadcastGoodsDO::getGoodsName, param.getGoodsName());
        List<BroadcastGoodsDO> dos = broadcastGoodsMapper.selectList(queryWrapper);
        if (Collections3.isNotEmpty(dos)) {
            throw new BusinessException("已经存在相同名称的分组!");
        }
    }
}
